<?php

/**
 * @author aisyah
 * @copyright 2012
 */


    

require_once "../../app/dao/database.php";
require_once "../../config/tanggal.php";

$class_db = new Database;
$tgl = new Tanggal;

$content;

$modul = $_POST['jenis'];
$format = $_POST['format'];

switch($modul){
    
    case "1":
    $class_db->connect->connect();

    $query = "SELECT product, price, jumlah, total FROM order_detail WHERE store = 1 GROUP BY product";
    
    //print_r($query);
    
    $result = mysql_query($query);

    $content = "<table>
            <thead><th>No.</th><th>produk</th><th>harga satuan</th><th>jumlah</th><th>total</th></thead>";

    $no=1;
    while($r=mysql_fetch_array($result)){
    
        $q = "SELECT product_name FROM product_detail WHERE productdet_id = ".$r['product'];
        
        $res = mysql_query($q);
        
        if($w=mysql_fetch_array($res)){
            $produk = $w['product_name'];
        }
    
        $harga = number_format($r['price'],2,',','.');
        $total = number_format($r['total'],2,',','.');
        $content .= "
                    <tr>
                    <td>$no</td>
                    <td>$produk</td>
                    <td>$harga</td>
                    <td>$r[jumlah]</td>
                    <td>$total</td>
                    </tr>
                    ";
        $no++;
    }

    $content .= "</table>";
       
    $class_db->connect->disconnect();
    
    break;
    
    case "2":
    
        $class_db->connect->connect();

    $query = "SELECT corder.customer as customer, corder.ship_addr as addr,                     
                    SUM(order_detail.total) as total 
                    FROM corder INNER JOIN order_detail ON corder.order_id = order_detail.order_id
                    WHERE store = 1 GROUP BY customer";
    
    //print_r($query);
    
    $result = mysql_query($query);

    $content = "<table>
            <thead><th>No.</th><th>customer</th><th>Alamat</th><th>Jumlah Pembelian</th></thead>";

    $no=1;
    while($r=mysql_fetch_array($result)){
    
        $q = "SELECT name FROM customer WHERE customer_id = ".$r['customer'];
        
        $res = mysql_query($q);
        
        if($w=mysql_fetch_array($res)){
            $customer = $w['name'];
        }
            
        $total = number_format($r['total'],2,',','.');
        $content .= "
                    <tr>
                    <td>$no</td>
                    <td>$customer</td>
                    <td>$r[addr]</td>                    
                    <td>$total</td>
                    </tr>
                    ";
        $no++;
    }

    $content .= "</table>";
       
    $class_db->connect->disconnect();
        
    break;
    
    case "3":
    
    //$bulan = date('m');
    
    $class_db->connect->connect();

    
    $query = "SELECT corder.order_id,
                    order_detail.product as product,
                    SUM(order_detail.total) as total,
                    corder.order_date as date,                      
                    corder.customer as customer
                    FROM corder INNER JOIN order_detail ON corder.order_id = order_detail.order_id
                    WHERE store = 1  GROUP BY product";
    
    $data = $class_db->select_table($query);
    //print_r($query);
    
    //$result = mysql_query($query);

    $content = "<table border=1>
            <thead><th>No.</th><th>Tanggal</th><th>customer</th><th>Jumlah Pembelian</th></thead>";

    $no=1;
    $total = 0;
    $produk = "";
    $cek = false;
    //$data = $class_db->hasil;
    foreach($data as $r){
    //while($r=mysql_fetch_array($result)){
        
        if($r['product']!=$produk){
            $s = "SELECT product_name FROM product_detail WHERE productdet_id = ".$r['product'];
            
            $rest = $class_db->select_table($s);
            
            foreach($rest as $x){
            //if($x=mysql_fetch_array($rest)){
                $prod = $x['product_name'];
            }
            
            $produk = $r['product'];
            
            $content .="<tr><td colspan=4><h4><b>Penjualan : $prod-$r[product]</b></h4></td></tr>";
            $cek = true;
        }else{
            $cek = false;
        }
    
        $q = "SELECT name FROM customer WHERE customer_id = ".$r['customer'];
        
        $res = $class_db->select_table($q);
        
        foreach($res as $w){
        //if($w=mysql_fetch_array($res)){
            $customer = $w['name'];
        }
            
        $jumlah = number_format($r['total'],2,',','.');
        $total += $r['total'];
        $tgl->set_tgl($r['date']);
        $tanggal = $tgl->get_tgl_indo();
        $content .= "
                    <tr>
                    <td>$no</td>
                    <td>$tanggal</td>
                    <td>$customer</td>                    
                    <td align=right>$jumlah</td>
                    </tr>
                    ";
        if($cek){
            $q1 = "SELECT SUM(total) as jumlah FROM order_detail WHERE product = ".$r['product']." AND store = 1";
        
            $res1 = $class_db->select_table($q1);
            
            foreach($res1 as $w1){
            //if($w1=mysql_fetch_array($res1)){
                $subjumlah = number_format($w1['jumlah'],2,',','.');
            }
            $content .= "<tr><td colspan=3 align=right><b>Sub Jumlah</b></td><td align=right><b>$subjumlah</b></td></tr></br>";
        }
        $no++;
    }
    
    $total = number_format($total,2,',','.');
    $content .= "<tr><td colspan=3 align=right><b>TOTAL PENJUALAN</b></td><td align=right><b>$total</b></td></tr>";

    $content .= "</table>";
       
    $class_db->connect->disconnect();
        
    break;
    
}
    
if($format=='1'){
    header("Content-type: application/x-msdownload");
    header("Content-disposition: attachment; filename=laporan.doc");
    header("Pragma: no-cache");
    header("Expires: 0");
    echo $content;
}else if($format=='2'){
    header("Content-type: application/x-msdownload");
    header("Content-disposition: attachment; filename=laporan.xls");
    header("Pragma: no-cache");
    header("Expires: 0");
    echo $content; }

?>